package com.ymatou.iapi.reachpromotion.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ymatou.iapi.reachpromotion.parameter.ProductDto;
import com.ymatou.iapi.reachpromotion.parameter.PromotionPolicy;
import com.ymatou.iapi.reachpromotion.parameter.PromotionPolicys;
import com.ymt.core.tool.Logger;
import com.ymttest.database.mongo.NewSellerPromotionInfoWapper;
import com.ymttest.utils.YMTDateUtil;

public class PromotionInfoVerify {

	public void verifyPromotionPolicyInDB(PromotionPolicy policy, boolean checkProduct) throws Exception
	{
		Logger.comment("验证"+policy.getPromotionId()+"与数据库一致");
		Map m = new NewSellerPromotionInfoWapper().getPromotionById(policy.getPromotionId());
		
		Logger.verifyEquals(m.get("des").toString(),policy.getPromotionDesc(), "验证PromotionDesc");
		Logger.verifyEquals(Integer.parseInt(m.get("sid").toString()),policy.getSellerId(), "验证PromotionDesc");
		Logger.verifyEquals(Integer.parseInt(m.get("type").toString()),policy.getPromotionType(), "验证PromotionType");
		Logger.verifyEquals(m.get("name").toString(),policy.getPromotionName(), "验证PromotionName");
		
		Logger.verifyEquals(YMTDateUtil.getFromDTSDate(m.get("bt").toString(), YMTDateUtil.YMDTHMS_STRING).substring(0, 19),policy.getPromotionBeginTime().substring(0, 19), "验证PromotionBeginTime");
		Logger.verifyEquals(YMTDateUtil.getFromDTSDate(m.get("et").toString(), YMTDateUtil.YMDTHMS_STRING).substring(0, 19),policy.getPromotionEndTime().substring(0, 19), "验证PromotionEndTime");
		
//		Logger.verifyEquals(YMTDateUtil.getFormatDateString(new Date(m.get("bt").toString())),policy.getPromotionBeginTime(), "验证PromotionBeginTime");
//		Logger.verifyEquals(YMTDateUtil.getFormatDateString(new Date(m.get("et").toString())),policy.getPromotionEndTime(), "验证PromotionEndTime");
		Logger.verifyEquals(0,policy.getPromotionLimitNum(), "验证PromotionLimitNum");
		Logger.verifyEquals(Boolean.parseBoolean(m.get("isuc").toString()),policy.isCanUseSellerCoupon(), "验证CanUseSellerCoupon");
		Logger.verifyEquals(Boolean.parseBoolean(m.get("issp").toString()),policy.isIsSameProductUse(), "验证IsSameProductUse");
		Logger.verifyEquals(Integer.parseInt(m.get("pjm").toString())==2,policy.isIsSpecifyProduct(), "验证IsSpecifyProduct");
		Logger.verifyEquals(Integer.parseInt(m.get("pjm").toString())==3,policy.isIsLimitProduct(), "验证IsLimitProduct");
//		Logger.verifyEquals(m.get("des").toString(),policy.g(), "验证Trace");

		List<Map> vss = (List<Map>) m.get("vs");
		for(int i=0;i<policy.getPromotionValues().size();i++)
		{
			Logger.verifyEquals(Float.parseFloat(vss.get(i).get("dv").toString()),policy.getPromotionValues().get(i).getValue(), "验证Value");

			if(Integer.parseInt(m.get("type").toString())==1||Integer.parseInt(m.get("type").toString())==2)
			{
				Logger.verifyEquals(Integer.parseInt(vss.get(i).get("rv").toString()),policy.getPromotionValues().get(i).getQtyCondition(), "验证QtyCondition");
			}
			
			if(Integer.parseInt(m.get("type").toString())==3||Integer.parseInt(m.get("type").toString())==4)
			{
				Logger.verifyEquals(Float.parseFloat(vss.get(i).get("rv").toString()),policy.getPromotionValues().get(i).getAmountCondition(), "验证AmountCondition");
			}
			
			if(Integer.parseInt(m.get("type").toString())==5)
			{
				Logger.verifyEquals(2,policy.getPromotionValues().get(i).getQtyCondition(), "验证QtyCondition");
				Logger.verifyEquals(2,Integer.parseInt(vss.get(i).get("rv").toString()), "验证数据库QtyCondition");
				Logger.verifyEquals((float)0.0,policy.getPromotionValues().get(i).getAmountCondition(), "验证AmountCondition");
			}
		}
		
		if(checkProduct)
		{
		List<Map> psm = (List<Map>) m.get("ps");
		List<String> productListInDB=new ArrayList<>();
		List<String> productListInReturn=new ArrayList<>();
		int i=0;
		if (psm != null) {
			for (Map map : psm) {
//				Logger.verifyEquals(map.get("pidg").toString(),policy.getPromotionProducts().get(i).getProductId(), "验证ProductId");
//				Logger.verifyEquals(policy.getPromotionId(),policy.getPromotionProducts().get(i).getPromotionId(), "验证PromotionId");
				productListInDB.add(map.get("pidg").toString());
				productListInReturn.add(policy.getPromotionProducts().get(i).getProductId());
				
				i++;
			}
		}
		
		Logger.verifyEquals(true, checkStringList(productListInDB,productListInReturn), "验证返回productid");
		}
		
		else {
			Logger.verifyEquals(0,policy.getPromotionProducts().size(), "验证PromotionProducts");

		}
	}
	public void verifyPromotionPolicyInDB(PromotionPolicy policy) throws Exception
	{
		verifyPromotionPolicyInDB(policy,true);
	
	}
	
	public boolean verifyPromotionPolicys(PromotionPolicys policy1,PromotionPolicys policy2)
	{
		List<PromotionPolicy> policies1 = policy1.getPromotionPolicys();
		List<PromotionPolicy> policies2 = policy2.getPromotionPolicys();

		if(policies1.size()!=policies2.size())
		{
				Logger.comment("PromotionPolicys条数不一致。");
				return false;
			
		}
		
		for(int i=0;i<policies1.size();i++)
		{
			if(!checkPromotionPolicyInList(policies2,policies1.get(i)))
			{
				return false;
			}
		}
		
		for(int i=0;i<policies2.size();i++)
		{
			if(!checkPromotionPolicyInList(policies1,policies2.get(i)))
				return false;
		}
		
		return true;
	}
	
	public boolean checkPromotionPolicyInList(List<PromotionPolicy> list, PromotionPolicy policy)
	{
		for (PromotionPolicy promotionPolicy : list) {
			if(checkPromotionPolicy(promotionPolicy,policy))
				return true;
		}
		
		return false;
	}
	
	public boolean checkPromotionPolicy(PromotionPolicy polic1,PromotionPolicy polic2)
	{
		if(polic1.getPromotionId()!=polic2.getPromotionId())
		{
			return false;
		}
		else {
			Logger.comment("验证PromotionId为"+polic1.getPromotionId()+"记录");
		}
		
		if(polic1.getSellerId()!=polic2.getSellerId())
		{
			Logger.comment("SellerId不一致："+polic1.getSellerId()+","+polic2.getSellerId());
			return false;
		}
		
		if(polic1.getPromotionType()!=polic2.getPromotionType())
		{
			Logger.comment("PromotionType不一致："+polic1.getPromotionType()+","+polic2.getPromotionType());
			return false;
		}
				
		if(!polic1.getPromotionName().equals(polic2.getPromotionName()))
		{
			Logger.comment("PromotionName不一致："+polic1.getPromotionName()+","+polic2.getPromotionName());
			return false;
		}
				
		if(!polic1.getPromotionDesc().equals(polic2.getPromotionDesc()))
		{
			Logger.comment("PromotionDesc不一致："+polic1.getPromotionDesc()+","+polic2.getPromotionDesc());
			return false;
		}
				
		if(!polic1.getPromotionBeginTime().substring(0, 18).equals(polic2.getPromotionBeginTime().substring(0, 18)))
		{
			Logger.comment("PromotionBeginTime不一致："+polic1.getPromotionBeginTime()+","+polic2.getPromotionBeginTime());
			return false;
		}
				
		if(!polic1.getPromotionEndTime().substring(0, 18).equals(polic2.getPromotionEndTime().substring(0, 18)))
		{
			Logger.comment("PromotionEndTime不一致："+polic1.getPromotionEndTime()+","+polic2.getPromotionEndTime());
			return false;
		}
				
		if(polic1.getPromotionLimitNum()!=polic2.getPromotionLimitNum())
		{
			Logger.comment("PromotionLimitNum不一致："+polic1.getPromotionLimitNum()+","+polic2.getPromotionLimitNum());
			return false;
		}
				
		if(polic1.isCanUseSellerCoupon()!=polic2.isCanUseSellerCoupon())
		{
			Logger.comment("CanUseSellerCoupon不一致："+polic1.isCanUseSellerCoupon()+","+polic2.isCanUseSellerCoupon());
			return false;
		}
				
		if(polic1.isIsSameProductUse()!=polic2.isIsSameProductUse())
		{
			Logger.comment("IsSameProductUse不一致："+polic1.isIsSameProductUse()+","+polic2.isIsSameProductUse());
			return false;
		}
				
		if(polic1.isIsLimitProduct()!=polic2.isIsLimitProduct())
		{
			Logger.comment("IsLimitProduct不一致："+polic1.isIsLimitProduct()+","+polic2.isIsLimitProduct());
			return false;
		}
				
		if(polic1.isIsSpecifyProduct()!=polic2.isIsSpecifyProduct())
		{
			Logger.comment("IsSpecifyProduct不一致："+polic1.isIsSpecifyProduct()+","+polic2.isIsSpecifyProduct());
			return false;
		}
				
		if(polic1.getPromotionValues().size()!=polic2.getPromotionValues().size())
		{
			Logger.comment("PromotionValues不一致：数量不一致");
			return false;
		}		
		else {
			for(int i=0;i<polic1.getPromotionValues().size();i++)
			{
				if(polic1.getPromotionValues().get(i).getQtyCondition()!=polic2.getPromotionValues().get(i).getQtyCondition())
				{
					Logger.comment("PromotionValues不一致："+polic1.getPromotionValues().get(i).getQtyCondition()+","+polic2.getPromotionValues().get(i).getQtyCondition());
					return false;
				}				
				if(polic1.getPromotionValues().get(i).getAmountCondition()!=polic2.getPromotionValues().get(i).getAmountCondition())
				{
					Logger.comment("AmountCondition不一致："+polic1.getPromotionValues().get(i).getAmountCondition()+","+polic2.getPromotionValues().get(i).getAmountCondition());
					return false;
				}					
				if(polic1.getPromotionValues().get(i).getValue()!=polic2.getPromotionValues().get(i).getValue())
				{
					Logger.comment("Value不一致："+polic1.getPromotionValues().get(i).getValue()+","+polic2.getPromotionValues().get(i).getValue());
					return false;
				}				}
		}
		
		
		if(polic1.getPromotionProducts()==null&&polic2.getPromotionProducts()==null)
		{
			return true;
		}
		else {
			if(polic1.getPromotionProducts()==null&&polic2.getPromotionProducts().size()==0)
			{
				return true;
			}
			else {
				if(polic1.getPromotionProducts().size()==0&&polic2.getPromotionProducts()==null)
				{
					return true;
				}
				else {
					if(polic1.getPromotionProducts().size()==polic2.getPromotionProducts().size())
					{
						//添加有值时的具体对比方法
						return true;
					}
					else {
						Logger.comment("PromotionProducts不一致");
						return false;
					}
				}
			}
		}
	}
	
	public boolean isStringInList(List<String> list, String str)
	{
		for (String string : list) {
			if(string.equals(str))
				return true;
		}
		return false;
	}
	
	public boolean checkStringList(List<String> list1, List<String> list2)
	{
		for (String string : list1) {
			if(!isStringInList(list2,string))
				return false;
		}
		
		for (String string : list2) {
			if(!isStringInList(list1,string))
				return false;
		}
		return true;
	}
	
	
}
